home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / NextDeveloper / Examples / DriverKit / Adaptec1542B / Adaptec1542B_reloc.tproj / AHARoutines.m < prev    next >
Encoding:
Text File  |  1995-02-10  |  5.2 KB  |  276 lines

  1. /*
  2.  * Copyright (c) 1992 NeXT Computer, Inc.
  3.  *
  4.  * AHARoutines.c - low-level I/O routines for Adaptec 1542 driver. 
  5.  *
  6.  * HISTORY
  7.  *
  8.  * 13 Apr 1993    Doug Mitchell at NeXT
  9.  *    Split off from AHAController.m.
  10.  */
  11.  
  12. #import "AHATypes.h"
  13. #import "AHAInline.h"
  14. #import "AHAThread.h"
  15. #import "AHAControllerPrivate.h"
  16. #import <driverkit/generalFuncs.h>
  17. #import <driverkit/kernelDriver.h>
  18.  
  19.  
  20. void aha_start_scsi(
  21.     unsigned short     base
  22. )
  23. {
  24.     aha_stat_reg_t    stat;
  25.  
  26.     do {
  27.         stat = aha_get_stat(base);
  28.     } while (stat.dataout_full);
  29.  
  30.     aha_put_cmd(base, AHA_CMD_START_SCSI);
  31. }
  32.  
  33.  
  34. boolean_t aha_probe_cmd(
  35.     unsigned short    base,
  36.     unsigned char    cmd,
  37.     unsigned char    *args,
  38.     int        arglen,
  39.     unsigned char    *reply,
  40.     int        replylen,
  41.     boolean_t    polled
  42. )
  43. {
  44.     aha_stat_reg_t    stat;
  45.     aha_intr_reg_t    intr;
  46.     boolean_t    success = FALSE;
  47.     int        fail_count = 100000;
  48.  
  49.     do {
  50.         stat = aha_get_stat(base);
  51.     } while (stat.dataout_full && fail_count--);
  52.     if (fail_count <= 0) 
  53.         return FALSE;
  54.  
  55.     aha_put_cmd(base, cmd);
  56.  
  57.     while (arglen-- > 0) {
  58.         fail_count = 100000;
  59.         do {
  60.             intr = aha_get_intr(base);
  61.             stat = aha_get_stat(base);
  62.             if (intr.cmd_done && stat.cmd_err)
  63.                 goto out;
  64.         } while (stat.dataout_full && fail_count--);
  65.         if (fail_count <= 0) 
  66.             return FALSE;
  67.  
  68.         aha_put_cmd(base, *args++);
  69.     }
  70.  
  71.     while (replylen-- > 0) {
  72.         fail_count = 100000;
  73.         do {
  74.             intr = aha_get_intr(base);
  75.             stat = aha_get_stat(base);
  76.             if (intr.cmd_done && stat.cmd_err)
  77.                 goto out;
  78.         } while (!stat.datain_full && fail_count--);
  79.         if (fail_count <= 0)
  80.             return FALSE;
  81.  
  82.         *reply++ = aha_get_cmd(base);
  83.     }
  84.     success = TRUE;
  85.  
  86.     fail_count = 100000;
  87.     if (polled) do {
  88.         intr = aha_get_intr(base);
  89.     } while (!intr.cmd_done && fail_count--);
  90.     if (fail_count <= 0) 
  91.         success = FALSE;
  92.  
  93. out:
  94.     if (polled)
  95.         aha_clr_intr(base);
  96.  
  97.     return (success);
  98. }
  99.  
  100. boolean_t aha_cmd(
  101.     unsigned short    base,
  102.     unsigned char    cmd,
  103.     unsigned char    *args,
  104.     int        arglen,
  105.     unsigned char    *reply,
  106.     int        replylen,
  107.     boolean_t    polled
  108. )
  109. {
  110.     aha_stat_reg_t    stat;
  111.     aha_intr_reg_t    intr;
  112.     boolean_t        success = FALSE;
  113.     
  114.     do {
  115.         stat = aha_get_stat(base);
  116.     } while (stat.dataout_full);
  117.  
  118.     aha_put_cmd(base, cmd);
  119.  
  120.     while (arglen-- > 0) {
  121.         do {
  122.             intr = aha_get_intr(base);
  123.             stat = aha_get_stat(base);
  124.             if (intr.cmd_done && stat.cmd_err) 
  125.                 goto out;
  126.         } while (stat.dataout_full);
  127.     
  128.         aha_put_cmd(base, *args++);
  129.     }
  130.  
  131.     while (replylen-- > 0) {
  132.         do {
  133.             intr = aha_get_intr(base);
  134.             stat = aha_get_stat(base);
  135.             if (intr.cmd_done && stat.cmd_err)
  136.                 goto out;
  137.         } while (!stat.datain_full);
  138.     
  139.         *reply++ = aha_get_cmd(base);
  140.     }
  141.     success = TRUE;
  142.     
  143.     if (polled) do {
  144.         intr = aha_get_intr(base);
  145.     } while (!intr.cmd_done);
  146.  
  147. out:
  148.     if (polled)
  149.         aha_clr_intr(base);
  150.     
  151.     return (success);
  152. }
  153.  
  154.  
  155. void aha_reset_board(
  156.     unsigned short     base, 
  157.     unsigned char     aha_board_id
  158. {
  159.  
  160.     aha_ctrl_reg_t    ctrl = { 0 };
  161.     aha_stat_reg_t    stat;
  162.  
  163.     ctrl.sw_rst = 1;
  164.  
  165.     aha_put_ctrl(base, ctrl);
  166.  
  167.     /* Avoid a 174x standard mode firmware bug */
  168.     if (aha_board_id == AHA_174xA) {
  169.         do {
  170.             stat = aha_get_stat(base);
  171.         } while (!stat.idle);
  172.         IOSleep(500);
  173.     }
  174.     else {
  175.         do {
  176.             stat = aha_get_stat(base);
  177.         } while (!stat.idle || !stat.mb_init_needed);
  178.     }
  179.  
  180.     aha_clr_intr(base);
  181. }
  182.  
  183. boolean_t aha_setup_mb_area(
  184.     unsigned short         base,
  185.     struct aha_mb_area     *ahaMbArea,
  186.     struct ccb         *ahaCcb
  187. )
  188. {
  189.     aha_cmd_init_t    init;
  190.     int            i;
  191.     unsigned        *mbPhysAddr;
  192.     IOReturn        rtn;
  193.     aha_mb_t        *mbOutVirt;
  194.     aha_mb_t        *mbInVirt;
  195.     struct ccb        *ccbPhys;
  196.     struct ccb        *ccbVirt;
  197.  
  198.     ddm_init("AHAController aha_setup_mb_area\n", 1,2,3,4,5);
  199.    
  200.     rtn = IOPhysicalFromVirtual(IOVmTaskSelf(),
  201.         (vm_address_t)ahaMbArea,
  202.         (unsigned *)&mbPhysAddr);
  203.     if(rtn) {
  204.         IOLog("AHAController: Can't get physical address of "
  205.             "ahaMbArea (%s)\n", [IODevice stringFromReturn:rtn]);
  206.         return FALSE;
  207.     }
  208.     aha_unlock_mb(base);
  209.  
  210.     init.mb_cnt = AHA_MB_CNT;
  211.     aha_put_24((vm_offset_t)mbPhysAddr, init.mb_area_addr); 
  212.  
  213.     if (!aha_cmd(base,
  214.         AHA_CMD_INIT, (unsigned char *)&init, sizeof (init),
  215.         0, 0, TRUE)) {
  216.         IOLog("aha at %x: mb_init failed\n", base);
  217.         return (FALSE);
  218.     }
  219.  
  220.     /*
  221.      * Setup CCBs and mailboxes. The CCB pointers in the mb_out mailboxes 
  222.      * have to be physical addresses; the mb_out pointer in the ccb is a 
  223.      * virtual address.
  224.      */
  225.     mbOutVirt = &ahaMbArea->mb_out[0];
  226.     mbInVirt = &ahaMbArea->mb_in[0];
  227.     ccbVirt = ahaCcb;
  228.     
  229.     for (i = 0; i < AHA_MB_CNT; i++) {
  230.     
  231.         mbOutVirt->mb_stat = AHA_MB_OUT_FREE;
  232.         mbInVirt->mb_stat  = AHA_MB_IN_FREE;
  233.         ccbVirt->mb_out    = mbOutVirt;
  234.  
  235.         rtn = IOPhysicalFromVirtual(IOVmTaskSelf(),
  236.             (vm_address_t)ccbVirt,
  237.             (unsigned *)&ccbPhys);
  238.         if(rtn) {
  239.             IOLog("AHAController: Can't get physical address of "
  240.             "ccb (%s)\n", [IODevice stringFromReturn:rtn]);
  241.             return FALSE;
  242.         }
  243.         ddm_init("ccbPhys[%d] = 0x%x\n", i, ccbPhys, 3,4,5);
  244.         
  245.         aha_put_24((vm_offset_t) ccbPhys, mbOutVirt->ccb_addr);
  246.     
  247.         mbOutVirt++;
  248.         mbInVirt++;
  249.         ccbVirt++;
  250.     }
  251.     
  252.     return (TRUE);
  253. }
  254.  
  255. void aha_unlock_mb(
  256.     unsigned short     base
  257. )
  258. {
  259.     aha_mb_lock_t    lock;
  260.  
  261.     /*
  262.      * Unlock the mailbox interface in case extended
  263.      * BIOS translation was used.
  264.      */
  265.     if(!aha_probe_cmd(base, AHA_CMD_GET_BIOS_INFO,
  266.         0, 0, (unsigned char*)&lock, sizeof(lock), TRUE)) {
  267.             return;
  268.     }
  269.  
  270.     lock.mb_status = 0;
  271.     aha_probe_cmd(base, AHA_CMD_SET_MB_ENABLE,
  272.         (unsigned char *)&lock, sizeof(lock), 0, 0, TRUE);
  273. }
  274.  
  275.